package com.example.hrsystem.data.source;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.hrsystem.data.model.AttendanceRecord;

import java.util.Date;
import java.util.List;

/**
 * 打卡记录数据访问对象
 * 提供对AttendanceRecord表的操作方法
 */
@Dao
public interface AttendanceDao {

    /**
     * 插入一条打卡记录
     */
    @Insert
    void insert(AttendanceRecord record);

    /**
     * 更新打卡记录
     */
    @Update
    void update(AttendanceRecord record);

    /**
     * 查询所有打卡记录
     */
    @Query("SELECT * FROM attendance_records ORDER BY checkInTime DESC")
    LiveData<List<AttendanceRecord>> getAllAttendanceRecords();

    /**
     * 根据员工ID查询打卡记录
     */
    @Query("SELECT * FROM attendance_records WHERE employeeId = :employeeId ORDER BY checkInTime DESC")
    LiveData<List<AttendanceRecord>> getAttendanceRecordsByEmployeeId(String employeeId);

    /**
     * 根据日期查询打卡记录
     */
    @Query("SELECT * FROM attendance_records WHERE date(checkInTime) = date(:date) AND employeeId = :employeeId")
    LiveData<AttendanceRecord> getAttendanceRecordByDate(Date date, String employeeId);

    /**
     * 查询指定日期范围内的打卡记录
     */
    @Query("SELECT * FROM attendance_records WHERE checkInTime BETWEEN :startDate AND :endDate AND employeeId = :employeeId ORDER BY checkInTime DESC")
    LiveData<List<AttendanceRecord>> getAttendanceRecordsByDateRange(Date startDate, Date endDate, String employeeId);

    /**
     * 查询指定日期范围内的打卡记录（同步版本）
     */
    @Query("SELECT * FROM attendance_records WHERE checkInTime BETWEEN :startDate AND :endDate AND employeeId = :employeeId ORDER BY checkInTime DESC")
    List<AttendanceRecord> getAttendanceRecordsByDateRangeSync(Date startDate, Date endDate, String employeeId);

    /**
     * 查询未完成打卡的记录
     */
    @Query("SELECT * FROM attendance_records WHERE isCompleted = 0 AND employeeId = :employeeId")
    LiveData<List<AttendanceRecord>> getUncompletedAttendanceRecords(String employeeId);

    /**
     * 查询今日签到记录（LiveData版本）
     */
    @Query("SELECT * FROM attendance_records WHERE date(checkInTime) = date('now') AND employeeId = :employeeId")
    LiveData<AttendanceRecord> getTodayAttendanceRecord(String employeeId);
    
    /**
     * 查询今日签到记录（同步版本）
     */
    @Query("SELECT * FROM attendance_records WHERE date(checkInTime) = date('now') AND employeeId = :employeeId")
    AttendanceRecord getTodayAttendanceRecordSync(String employeeId);

    /**
     * 删除所有打卡记录
     */
    @Query("DELETE FROM attendance_records")
    void deleteAll();
}